//
//  TNAutoCoverView.swift
//  CommonTool
//
//  Created by Name i on 2022/4/18.
//

import UIKit

class TNAutoCoverView: UIView {

    private lazy var backgroundImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "bg2")
        img.clipsToBounds = true
        return img
    }()

    private lazy var logo: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "123")
        img.clipsToBounds = true
        return img
    }()
    
    
    private lazy var contentContainer: UIView = {
        let view = UIView()
        return view
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 25)
        label.textColor = .white
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }()
    
    private var contentInset: UIEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        
        
        
        addSubviews(childs: [backgroundImageView, logo, contentContainer])
        contentContainer.addSubview(contentLabel)
        
        
        backgroundImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        contentLabel.snp.makeConstraints { make in
            make.left.right.centerY.equalToSuperview()
            make.top.greaterThanOrEqualToSuperview()
        }
        showLogoOrNot(show: false)
    }
    
    
    public func showLogoOrNot(show: Bool) {
        logo.isHidden = !show
        if show {
            logo.snp.remakeConstraints { make in
                make.top.equalToSuperview().offset(contentInset.top)
                make.left.equalToSuperview().offset(contentInset.left)
                make.size.equalTo(CGSize(width: 100, height: 20))
            }
            contentContainer.snp.remakeConstraints { make in
                make.top.equalTo(logo.snp.bottom).offset(10)
                make.left.equalToSuperview().offset(contentInset.left)
                make.right.equalToSuperview().offset(-contentInset.right)
                make.bottom.equalToSuperview().offset(-contentInset.bottom)
            }
        }else {
            logo.snp.removeConstraints()
            contentContainer.snp.remakeConstraints { make in
                make.edges.equalToSuperview().inset(contentInset)
            }
        }
    }
    
    public func updateContent(content: String?) {
        contentLabel.text = content
    }
    
}
